package com.hibernate;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class HibernateDao<T> extends HibernateDaoSupport {
	public void ExecuteProctype(final String procname,final int id) throws HibernateException, SQLException{
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Session session = dao.getHibernateTemplate().getSessionFactory().getCurrentSession();
		String procedure = "call "+procname+"(?)";
		SQLQuery sqlQuery  = session.createSQLQuery(procedure);
		sqlQuery.setInteger(0, id);
		sqlQuery.executeUpdate();
	}
	
	public void ExecuteProc(final String procname) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		String sql="call "+procname+"()";
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	
	public boolean addObject(T object) {
		boolean status=false;
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");		
		try {		
			dao.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); 					
			dao.getHibernateTemplate().save(object);
			dao.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
			status=true;
		} catch (HibernateException e) {
			status=false;
		}
		return status;
	}
	public void updateObject(T object) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);		
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); 
		dao.getHibernateTemplate().saveOrUpdate(object);
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
	}
	public void mergeObject(T object) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);		
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); 
		dao.getHibernateTemplate().merge(object);
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
	}

	public T getObject(int id,Class clazz) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		T object = (T) dao.getHibernateTemplate().get(clazz, id);
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<T> getObjects(String hql,final int offset,final int length) {   //�����б�ķ�ҳ
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		query.setFirstResult(offset);
        query.setMaxResults(length);
        List<T> list = query.list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<T> getListObjects(String hql) {   //��ݿ���ĳ���������
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		List<T> list =dao.getHibernateTemplate().find(hql);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<T> getListObjects(String hql,int rownum) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);		
		//Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		//Transaction tx = session.beginTransaction();
		//Query query = session.createQuery(hql);
		query.setMaxResults(rownum);
		List<T> list = query.list();
	    

//		Query query = session.createQuery(hql);
//		query.setMaxResults(rownum);
//        List<T> list = query.list();
		//tx.commit();
		//Session session=super.getSessionFactory().getCurrentSession();
		return list;
	}	
	
	public int getAllRowCount(String hql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(hql);
		int size=(new BigInteger(query.uniqueResult().toString())).intValue();
		return size;
    }
	@SuppressWarnings("unchecked")
	public void deleteObject(int id,Class clazz) {
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); 
		T object = getObject(id,clazz);
		dao.getHibernateTemplate().delete(clazz.getName(), object);
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
//		T object = getObject(id,clazz);
//		dao.getHibernateTemplate().delete(clazz.getName(), object);
	}
	public void update(String sql){//������ݿ���ָ���ֶ�
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql).executeUpdate();
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
		//dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(sql).executeUpdate();
    }	
	public int ExecuteHQL(String hql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		int result=dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql).executeUpdate();
		return result;
	}
	public List ExecuteSQL(String SQL){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		List list = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(SQL).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		return list;
	}
	public Object getValue(String Sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql);
		Object result=query.uniqueResult();
		return result;
	}
	public Map getMap(String Sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List list=query.list();
		Map result=new HashMap();
		if(list!=null&&list.size()>0)result=(Map)list.get(0);
		return result;
	}
	public List<Map> getList(String Sql,final int offset,final int length){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		query.setFirstResult(offset);
        query.setMaxResults(length);		
        List<Map> result=query.list();
		return result;
	}
	public List<Map> getList(String Sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        List<Map> result=query.list();
		return result;
	}	
	
	public void executesql(String sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
	public List getNlist(String Sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		List list=query.list();
		return list;
	}
	public List<List> getListWithOutMap(String Sql){
		ServletContext scontext=ServletActionContext.getServletContext();
		WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(scontext);
		HibernateDao<T> dao=(HibernateDao<T>)context.getBean("hibernateDao");
		Query query = dao.getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(Sql).setResultTransformer(Transformers.TO_LIST);
        List<List> result=query.list();
		return result;
	}	
}
